package com.ljb.nfc_demo.nfc;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.ljb.nfc_demo.R;

/**
 * Created by ljb on 2017/7/13.
 */

public class ReadActivity extends BaseNFCActivity implements View.OnClickListener {
    private static final String TAG = "ReadActivity";
    private TextView tv_show;

    public static void actionStart(Context context) {
        context.startActivity(new Intent(context, ReadActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read);
        initUI();
    }

    private void initUI() {
        tv_show = (TextView) findViewById(R.id.tv_show);
        findViewById(R.id.bt_read).setOnClickListener(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.e(TAG, "onNewIntent: 发现新的intent对象" + intent.getAction());
        if (isRead) {
            Toast.makeText(this, "检测到NFC标签,开始读取标签数据", Toast.LENGTH_SHORT).show();
            String data = nfcUtils.read(intent);
            if (data != null) {
                read_data += "\r\n" + data;
                tv_show.setText(read_data);
                Toast.makeText(this, "读取数据成功:" + read_data, Toast.LENGTH_SHORT).show();
            }
        }
    }

    private boolean isRead = false;
    private String read_data = "";

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.bt_read) {//读
            Toast.makeText(this, "开始扫描NFC标签", Toast.LENGTH_SHORT).show();
            isRead = true;
        }
    }
}
